package com.hlbspringcloud.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Author huanglibin
 * @Project mq
 * @Description 发布订阅模式 消息生产者
 * @Date 2020-12-04-09-29-08
 * @Version 1.0
 */
public class PublishSender {
    /**
     * 路由名称
     */
    public static final String ROUNTING_NAME = "publish rounting";

    /**
     * 发布消息
     * @throws Exception
     */
    public static void sendMessage() throws Exception{
        //1.获取连接
        Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
        //2.创建管道
        Channel channel = rabbitmqConnection.createChannel();
        //3.创建路由（参数1：路由名称；参数2：模式名称，fanout表示发布订阅模式）
        channel.exchangeDeclare(ROUNTING_NAME,"fanout");
        //4.设置每次发送消息不超过1条
        channel.basicQos(1);
        String message="hello ,I am Publish/Subscribe";
        //5.发布消息
        channel.basicPublish(ROUNTING_NAME,"",null,message.getBytes());
    }

    public static void main(String[] args) throws Exception {
        PublishSender.sendMessage();
    }
}
